package com.yehaikeji.workflow.controller.management;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehaikeji.workflow.bean.CountBean;
import com.yehaikeji.workflow.bean.thing;
import com.yehaikeji.workflow.controller.bean.resultlist;
import com.yehaikeji.workflow.service.thingService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RequestMapping("countManager")
@Controller
public class countinforController {
	
	@Autowired
	private thingService thingService;
	
	@RequestMapping(value = "/manage")
	private String manageradmin(ModelMap map) {
		map.put("ismenu",2);
		return "count";
	}
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "统计列表")
	public resultlist list(@RequestParam Integer page, @RequestParam Integer limit,@RequestParam Integer typeid,String startDate,String endDate) {
		resultlist resultMessage = new resultlist();
		try {
			if (page <= 0 || limit <= 0) {
				resultMessage.setMsg("pagelimit参数错误");
				resultMessage.setCode(1);
			} else if(typeid==null||typeid<=0||typeid>4){
				resultMessage.setMsg("typeid参数错误");
				resultMessage.setCode(1);
			}{
				PageHelper.startPage(page, limit);
				List<CountBean> countBeanList = thingService.selectCount(startDate, endDate,typeid);
				if(countBeanList.get(0)==null) {
					countBeanList.remove(0);
				}
				PageInfo<CountBean> thinginfor = new PageInfo<>(countBeanList);
				
				resultMessage.setCount(thinginfor.getTotal());
				resultMessage.setData(thinginfor.getList());
				resultMessage.setCode(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}
	
	@RequestMapping(value = "countselect", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "统计分类查询（反查）",notes="时间为空默认所有，单位参数如果为空则默认总计")
	public resultlist countselectlist(@RequestParam Integer page, @RequestParam Integer limit,@RequestParam Integer typeid,String startDate,String endDate,String unitida,@ApiParam("区分表头列的参数classify=1总计，2办理中，3已经办结,4满意，5基本满意，6不满意,7未评价")@RequestParam Integer classify) {
		resultlist resultMessage = new resultlist();
		try {
			if (page <= 0 || limit <= 0) {
				resultMessage.setMsg("参数错误");
				resultMessage.setCode(1);
			} else {
				System.out.println("typeid:"+typeid+"startDate:"+startDate+"unitida:"+unitida+"classify:"+classify);
				PageHelper.startPage(page, limit);
				List<thing> things = thingService.countselectlist(typeid,startDate,endDate,unitida,classify);
				PageInfo<thing> thinginfor = new PageInfo<>(things);

				resultMessage.setCount(thinginfor.getTotal());
				resultMessage.setData(thinginfor.getList());
				resultMessage.setCode(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}
	
	
}
